package com.tanguy.driver

import android.content.Context
import android.support.multidex.MultiDex
import cn.sinata.xldutils_kotlin.application.BaseApplication
import cn.sinata.xldutils_kotlin.utils.SPUtils
import com.tanguy.driver.bean.DriverRegister
import com.tanguy.driver.net.socket.OkioSocket
import com.tanguy.driver.net.socket.SocketManager
import com.tanguy.driver.utlis.StackManager
import com.tanguy.driver.utlis.extend.rxTimer
import io.reactivex.disposables.Disposable

/**
 * Created by dab on 2017/9/26 0026 14:31
 */
class App : BaseApplication() {
    override fun getSPName(): String {
        return "m_data"
    }

    companion object {
        lateinit var app: App
        fun get(): App = app
    }
    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }
    var userId = 0
    var latitude = ""//司机定位的上一次位置
    var longitude = ""
    var code = ""//排队的code
    var isQueue=false//是否在排队中
    var lastAreaID=0//上个区域id
    var lastAreaType=0//上个区域type
    val driverRegister by lazy {
        DriverRegister()
    }

    override fun onCreate() {
        super.onCreate()
        app = this
        initSocket()
        StackManager.initStackManager(this)
    }
    /**
     *  登出用户
     */
    fun logOut() {
        userId = 0
        latitude = ""//司机定位的上一次位置
        longitude = ""
        code = ""//排队的code
        isQueue=false//是否在排队中
        lastAreaID=0//上个区域id
        lastAreaType=0//上个区域type
        SPUtils.instance()
                .put(Const.User.PASSWORD, "")
                .put(Const.User.PHONE,"")
                .apply()
    }

    ////    语音
//// /////////////////////////////////////////
//private val speaking by lazy {
//    TTSController.getInstance(applicationContext).apply {
//        init()
//        startSpeaking()
//    }
//}
//    fun speaking(playText:String) {
//        speaking.playText(playText)
//    }
//    fun speaking(orderType:String,start:String,end:String) {
//
//        val format = String.format("您收到新的%s，从%s出发，去往%s", orderType, start, end)
//        speaking.playText(format)
//    }
///////////////////////////////////////////////////////////////////
    //Socket相关
    private val okioSocket = OkioSocket()

    private fun initSocket() {
        okioSocket.apply {
            var rxTimerDisposable: Disposable? = null
            setOnConnectCallBack({ succeed, _, _ ->
                if (succeed) {
                    SocketManager.ping()
                    rxTimerDisposable = rxTimer(Const.SocketType.INTERVAL) {
//                        SocketManager.ping()
                    }
                } else {
                    rxTimerDisposable?.dispose()
                }
            }
            )
        }
        okioSocket.setOnMessageChange { SocketManager.parseJson(it) }
    }

    open fun sendMassage(sendMsg: String) {
        okioSocket.send(sendMsg)
    }

    open fun connectSocket() {
        okioSocket.connect(Const.Api.SOCKET_SERVER, Const.Api.SOCKET_PORT)
    }

    /**
     * 取消连接
     */
    open fun offSocket() {
        try {
            okioSocket.close()
        } catch (e: Exception) {

        }
    }
}